<?php

namespace App\Http\Controllers\System\Testing;

use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use App\Http\Resources\Common\DataResource;
use App\Services\Postman\ApiService as PostmanApiService;

class PlayerController extends Controller
{

    /**
     * 玩家登录
     *
     * @param Request $request
     * @return DataResource
     */
    public function login(Request $request): DataResource
    {
        list($rules, $messages) = $this->validateAccount();
        // 参数验证
        $data = $request->validate([
                'log_level' => 'required|integer',
                'lang' => 'required|string',
                'game_id' => 'nullable|string',
                'plat_code' => 'required|string',
                'currency' => 'nullable|string',
            ] + $rules, $messages
        );

        if (isset($data['currency'])) {
            $data['currency'] = explode(',',$data['currency']);
        }

        $res = PostmanApiService::post(PostmanApiService::URI_LOGIN,
            is_null($data['game_id']) ? Arr::except($data, 'game_id') : $data
        );

        return new DataResource($res);
    }

    /**
     * 玩家退出
     *
     * @param Request $request
     * @return DataResource
     */
    public function logout(Request $request): DataResource
    {
        list($rules, $messages) = $this->validateAccount();
        // 参数验证
        $data = $request->validate([
                'plat_code' => 'required|string',
            ] + $rules, $messages
        );

        $res = PostmanApiService::post(PostmanApiService::URI_LOGOUT, $data);

        return new DataResource($res);
    }
}
